<!--  -->
<template>
  <div>
    <el-card>
      <div slot="header">
        <span>条件搜索</span>
      </div>
      <!-- card body -->
    </el-card>

    <el-card>
      <div slot="header">
        <span>数据表格</span>
      </div>
      <el-button type="primary" @click="goAdd">新增</el-button>
    </el-card>

    <el-table v-loading="loading" :data="newsList" border style="width: 100%">
      <el-table-column label="#" type="index" width="50" fixed />
      <el-table-column prop="title" label="标题" width="150" />
      <el-table-column prop="author" label="作者" width="150" />
      <el-table-column prop="desc" label="描述" width="120" />
      <el-table-column prop="addTime" label="开始时间" width="120" />
      <el-table-column label="封面图片" :width="300">
        <template slot-scope="scope">
          <img class="cover-img" :src="scope.row.cover" alt />
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" >
        <!-- 自定义列的实现  作用于插槽-->
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="cover"></el-image>
        </template>
        <template slot-scope="scope">
          <div style="display:flex;justify-content:space-around;">
            <el-button type="primary" @click="goEdit(scope.row.id)">编辑</el-button>
            <el-button type="danger" @click="doDele(scope.row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页部分 -->
    <el-pagination
      style="margin-top: 30px;text-align:right;"
      :current-page="page.start"
      :page-sizes="page.sizes"
      :page-size="page.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { getNewsList, dele } from "@/api/material/news";
export default {
  name: "news",
  data() {
    return {
      loading: false,
      newsList: [],
      page: {
        start: 1,
        limit: 12,
        totalCount: 0,
        sizes: [12, 24, 36], // 注意: element 的sizes必须是limit的整倍数
      },
      search: {},
    };
  },

  computed: {},
  created() {
    this.getnewsList();
  },
  mounted() {},
  methods: {
    check(e){
      console.log(e)
    },
    goAdd() {
      this.$router.push({ name: "xynewsAdd" });
    },
    goEdit(e) {
      this.$router.push({ name: "matNewsEdit", params: { id: e } });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.page.limit = val;
      this.getnewsList();
    },
    handleCurrentChange(val) {
      // 页码发生了变化 需要改变start重新发送请求
      console.log(`当前页: ${val}`);
      this.page.start = val;
      this.getnewsList();
    },
    doDele(e) {
      dele({ id: e }).then((res) => {
        this.getnewsList();
      });
    },
    getnewsList() {
      getNewsList({
        start: this.page.start,
        limit: this.page.limit,
        ...this.search,
      }).then((res) => {
        //  console.log(res);
        this.newsList = res.data.list;
        this.page.totalCount = res.data.totalRow;
      });
    },
  },
};
</script>

<style lang='scss' scoped >
.cover-img {
  width: 200px;
  height: 150px;
}
</style>
